﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Extend.Pool;

[CustomEditor(typeof(PoolManager))]
public class PoolManagerInspector : Editor
{
    private HashSet<string> _openItems = new HashSet<string>();

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        if (!EditorApplication.isPlaying)
        {
            EditorGUILayout.HelpBox("仅运行时可用", MessageType.Info);
            return;
        }

        PoolManager poolManager = (PoolManager)target;

        List<PoolBase> unityObjPools = poolManager.GetAllUnityPools();
        EditorGUILayout.LabelField("Unity Object Pool Count:", unityObjPools.Count.ToString());
        foreach (var item in unityObjPools)
        {
            DrawPool(item);
        }

        unityObjPools = poolManager.GetAllReferencePools();
        EditorGUILayout.LabelField("Reference Pool Count:", unityObjPools.Count.ToString());
        foreach (var item in unityObjPools)
        {
            DrawPool(item);
        }

        Repaint();
    }

    private void DrawPool(PoolBase objectPool)
    {
        bool lastState = _openItems.Contains(objectPool.Name);
        bool currentState = EditorGUILayout.Foldout(lastState, objectPool.Name);
        if (currentState != lastState)
        {
            if (currentState)
            {
                _openItems.Add(objectPool.Name);
            }
            else
            {
                _openItems.Remove(objectPool.Name);
            }
        }
        if (currentState)
        {
            EditorGUILayout.BeginVertical("box");
            {
                EditorGUILayout.LabelField("Name:", objectPool.Name);
                EditorGUILayout.LabelField("Capacity:", objectPool.Capacity.ToString());
                EditorGUILayout.LabelField("Count:", objectPool.Count.ToString());
                EditorGUILayout.LabelField("Unused Count:", objectPool.CountUnUsed.ToString());
                EditorGUILayout.LabelField("Using Count:", (objectPool.Count - objectPool.CountUnUsed).ToString());
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.Separator();
        }

    }
}
